import 'package:flutter/material.dart';
import 'package:industry_net/base/common/util.dart';
import 'package:lottie/lottie.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class RefreshHeaderFooter {
  static ClassicHeader classicHeader() {
    return ClassicHeader(
      height: SizeFit.statusBarHeight + 44,
    );
  }

  static ClassicFooter classicFooter() {
    return ClassicFooter(
      height: SizeFit.safeBottom + 40,
    );
  }

  static CustomHeader customHeader({double? topInset}) {
    return CustomHeader(
      height: 60.r + (topInset ?? 0),
      builder: (context, mode) {
        Widget body = const SizedBox();

        if (mode == RefreshStatus.idle) {
          body = Text(
            "下拉刷新",
            style: pfMediumStyle(color: ColorsUtil.title, fontSize: font13),
          );
        } else if (mode == RefreshStatus.canRefresh) {
          body = Text(
            "松开刷新",
            style: pfMediumStyle(color: ColorsUtil.title, fontSize: font13),
          );
        } else if (mode == RefreshStatus.completed) {
          // body = Text(
          //   "刷新完成",
          //   style: pfMediumStyle(color: ColorsUtil.title, fontSize: font13),
          // );
          body = LottieBuilder.asset(
            'assets/jsons/loading_success.json',
            animate: true,
            repeat: false,
            reverse: false,
            width: 200.r,
            height: 60.r,
          );
        } else if (mode == RefreshStatus.refreshing) {
          body = LottieBuilder.asset(
            'assets/jsons/loading.json',
            animate: true,
            repeat: true,
            reverse: true,
            width: 200.r,
            height: 60.r,
          );
        }
        return Container(
          height: 60.r + (topInset ?? 0),
          margin: EdgeInsets.only(top: (topInset ?? 0)),
          color: ColorsUtil.white,
          alignment: Alignment.center,
          child: body,
        );
      },
    );
  }

  static void stop(
    RefreshController controller, {
    bool? isSuccess = true,
    bool? hasMoreData = true,
  }) {
    if (controller.isRefresh) {
      if (isSuccess == true) {
        if (hasMoreData == true) {
          controller.refreshCompleted();
        } else {
          controller.resetNoData();
        }
      } else {
        controller.refreshFailed();
      }
    }
    if (controller.isLoading) {
      if (isSuccess == true) {
        if (hasMoreData == true) {
          controller.loadComplete();
        } else {
          controller.loadNoData();
        }
      } else {
        controller.loadFailed();
      }
    }
  }
}
